<template>
  <!-- #ifdef APP-NVUE -->
  <text :style="{ color: color, 'font-size': iconSize }" class="com-icon" @click="_onClick">{{ unicode }}</text>
  <!-- #endif -->
  <!-- #ifndef APP-NVUE -->
  <text
    :style="{ color: color, 'font-size': iconSize }"
    class="com-icon"
    :class="['icon-' + name, customPrefix, customPrefix ? name : '']"
    @click="_onClick"
  />
  <!-- #endif -->
</template>

<script>
import icons from './icons.js'
const getVal = (val) => {
  const reg = /^[0-9]*$/g
  return typeof val === 'number' || reg.test(val) ? val + 'px' : val
}
// #ifdef APP-NVUE
var domModule = weex.requireModule('dom')
import iconUrl from './iconfont.ttf'
domModule.addRule('fontFace', {
  fontFamily: 'com-icon',
  src: "url('" + iconUrl + "')",
})
// #endif

/**
 * Icons 图标
 * @description 用于展示 icons 图标
 * @property {Number} size 图标大小
 * @property {String} name 图标图案
 * @property {String} color 图标颜色
 * @property {String} customPrefix 自定义图标
 * @event {Function} click 点击 Icon 触发事件
 */
export default {
  name: 'Com-Icon',
  emits: ['click'],
  props: {
    name: {
      type: String,
      default: '',
    },
    color: {
      type: String,
      default: '',
    },
    size: {
      type: [Number, String],
      default: 16,
    },
    customPrefix: {
      type: String,
      default: '',
    },
    stop: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      icons: icons.unicode,
    }
  },
  computed: {
    unicode() {
      let code = this.icons.find((v) => v.font_class === this.type)
      if (code) {
        return unescape(`%u${code.unicode}`)
      }
      return ''
    },
    iconSize() {
      return getVal(this.size)
    },
  },
  methods: {
    _onClick(e) {
      this.$emit('click')
      if (!this.stop && e) {
        e.stopPropagation()
      }
    },
  },
}
</script>

<style lang="scss">
/* #ifndef APP-NVUE */
@import './iconfont.css';
@font-face {
  font-family: com-icon;
  src: url('./iconfont.ttf') format('truetype');
}

/* #endif */

.com-icon {
  color: $com-color-content;
  text-align: center;
  text-decoration: none;
}
</style>
